Back to course home
0% completed
Vote For New Content
Google Medium
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Google Hard
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Count Nodes Equal to Average of Subtree (easy)
Problem Statement
Given the root of a binary tree, return the integer value representing the number of nodes where the node's value is equal to the average of the values in its subtree.
A subtree of the root node contains the root node and all of its descendants.
The average value of n elements is totalSum / n and rounded down to the nearest integer.
Examples
Example 1:
- Input:
[3,1,4,null,2]
- Expected Output:
3 - Justification: The node with value 3 has an average of (3+1+4+2)/4 = 2.5 (round down to 2), which doesn't match its value. The node with value 1 has an average of (1+2)/2 = 1.5 (round down to 1), matching its value. The node with value 4 has no children, so its average is 4, matching its value. The node with value 2 is a leaf, so its average is 2, matching its value. Therefore, there are 3 nodes whose value matches the average of their subtree.
Example 2:
- Input:
[5,4,8,3,null,6,7]
- Expected Output:
4 - Justification: The root node 5 has an average of (5+4+8+3+6+7)/6 = 5.5 (round down to 5), which matches its value. All leaves with values 3, 6, and 7 match their averages (as they have no children). So, there are a total 4 nodes that matches the given criteria.
Example 3:
- Input:
[10,20,30,40,50,60,70]
- Expected Output:
4 - Justification: The leaf nodes (40, 50, 60, 70) have their values equal to their averages as they do not have any children. The other nodes' values do not match the average of their subtrees. Therefore, there are 4 nodes whose values match the average of their subtree.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Monotonic Array
Solution: Count Nodes Equal to Average of Subtree
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself